﻿using DGIS.Main.Forms.Data;
using DGIS.Service.Model;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DGIS.Main
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        #region 事件

        #region 数据

        private void openShpMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog()
            {
                Filter = "shp|*.shp"
            };
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                DGIS.Service.IService.IDataService.IDataOpenService dataOpenService = new DGIS.Service.Service.DataService.DataOpenService();
                IFeatureLayer featureLayer = dataOpenService.OpenFromShp(dlg.FileName);

                this.axMapControl.AddLayer(featureLayer);
            }
        }

        private void openMDBMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog()
            {
                Filter = "mdb|*.mdb"
            };
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                DGIS.Service.IService.IDataService.IDataOpenService dataOpenService = new DGIS.Service.Service.DataService.DataOpenService();
                IWorkspace workspace;
                List<DataLayer> layers = dataOpenService.GetLayerList(dlg.FileName, Service.Enum.WorkspaceTypeEnum.Mdb, out workspace);

                if (workspace == null)
                {
                    MessageBox.Show("数据库打开失败");
                    return;
                }

                LayersFrm frm = new LayersFrm(layers);
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    string layerName = frm._selectedLayerName;
                    IFeatureLayer layer = new FeatureLayerClass()
                    {
                        Name = layerName,
                        FeatureClass = (workspace as IFeatureWorkspace).OpenFeatureClass(layerName)
                    };

                    this.axMapControl.AddLayer(layer);
                }
            }
        }

        private void openGdbMenuItem_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                DGIS.Service.IService.IDataService.IDataOpenService dataOpenService = new DGIS.Service.Service.DataService.DataOpenService();
                IWorkspace workspace;
                List<DataLayer> layers = dataOpenService.GetLayerList(dlg.SelectedPath, Service.Enum.WorkspaceTypeEnum.GeoDatabase, out workspace);
                if (workspace == null)
                {
                    MessageBox.Show("数据库打开失败");
                    return;
                }

                LayersFrm frm = new LayersFrm(layers);
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    string layerName = frm._selectedLayerName;
                    IFeatureLayer layer = new FeatureLayerClass()
                    {
                        Name = layerName,
                        FeatureClass = (workspace as IFeatureWorkspace).OpenFeatureClass(layerName)
                    };

                    this.axMapControl.AddLayer(layer);
                }
            }
        }

        private void openSDEMenuItem_Click(object sender, EventArgs e)
        {
            SdeFrm sdeFrm = new SdeFrm();
            if (sdeFrm.ShowDialog() == DialogResult.OK)
            {
                DGIS.Service.IService.IDataService.IDataOpenService dataOpenService = new DGIS.Service.Service.DataService.DataOpenService();
                IWorkspace workspace;
                List<DataLayer> layers = dataOpenService.GetLayerList(sdeFrm._server, sdeFrm._user, sdeFrm._psw, sdeFrm._dbName, sdeFrm._type, out workspace);

                if (workspace == null)
                {
                    MessageBox.Show("SDE连接失败");
                    return;
                }

                LayersFrm frm = new LayersFrm(layers);
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    string layerName = frm._selectedLayerName;
                    IFeatureLayer layer = new FeatureLayerClass()
                    {
                        Name = layerName,
                        FeatureClass = (workspace as IFeatureWorkspace).OpenFeatureClass(layerName)
                    };

                    this.axMapControl.AddLayer(layer);
                }
            }
        }

        private void memoryMenuItem_Click(object sender, EventArgs e)
        {
            DGIS.Service.IService.ICoordService.ISpatialReferenceService spatialReferenceService = new DGIS.Service.Service.CoordService.SpatialReferenceService();
            ISpatialReference spatialReference = spatialReferenceService.CreateSpatialReference(esriSRGeoCSType.esriSRGeoCS_WGS1984);

            DGIS.Service.IService.IDataService.IDataCreateService dataCreateService = new DGIS.Service.Service.DataService.DataCreateService();

            Dictionary<string, esriFieldType> dic = new Dictionary<string, esriFieldType>();
            dic.Add("name", esriFieldType.esriFieldTypeString);

            IFeatureClass featureClass = dataCreateService.CreateMemoryFeatureClass(spatialReference, esriGeometryType.esriGeometryPoint, dic);

            IFeatureLayer layer = new FeatureLayerClass()
            {
                Name = "Test",
                FeatureClass = featureClass
            };
            this.axMapControl.AddLayer(layer);
        }

        private void shpMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog()
            {
                Filter = "shp|*.shp"
            };
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                DGIS.Service.IService.ICoordService.ISpatialReferenceService spatialReferenceService = new DGIS.Service.Service.CoordService.SpatialReferenceService();
                ISpatialReference spatialReference = spatialReferenceService.CreateSpatialReference(esriSRGeoCSType.esriSRGeoCS_WGS1984);

                DGIS.Service.IService.IDataService.IDataCreateService dataCreateService = new DGIS.Service.Service.DataService.DataCreateService();

                Dictionary<string, esriFieldType> dic = new Dictionary<string, esriFieldType>();
                dic.Add("name", esriFieldType.esriFieldTypeString);

                IFeatureClass featureClass = dataCreateService.CreateShpFeatureClass(spatialReference, esriGeometryType.esriGeometryPoint, dic, dlg.FileName);

                IFeatureLayer layer = new FeatureLayerClass()
                {
                    Name = "Test",
                    FeatureClass = featureClass
                };
                this.axMapControl.AddLayer(layer);
            }
        }

        #endregion

        #region 渲染
        private void featureUniqueMenuItem_Click(object sender, EventArgs e)
        {
            IFeatureLayer featureLayer;
            if(this.axMapControl.LayerCount==0||(featureLayer=this.axMapControl.get_Layer(0) as IFeatureLayer) == null)
            {
                MessageBox.Show("请加载第一图层为矢量图层");
                return;
            }

            DGIS.Service.IService.IRenderService.ISymbolService symbolService = new DGIS.Service.Service.RenderService.SymbolService();
            string stylePath = @"C:\Users\Dview\Desktop\test.style";
            Dictionary<string, ISymbol> dic = new Dictionary<string, ISymbol>();
            dic.Add("核心区", symbolService.GetSymbolFromStyleFile(stylePath,"Fill Symbols", "核心区"));
            dic.Add("缓冲区", symbolService.GetSymbolFromStyleFile(stylePath, "Fill Symbols", "缓冲区"));
            dic.Add("实验区", symbolService.GetSymbolFromStyleFile(stylePath, "Fill Symbols", "实验区"));

            List<string> labels = new List<string>();
            labels.Add("核心区");
            labels.Add("缓冲区");
            labels.Add("实验区");

            DGIS.Service.IService.IRenderService.IFeatureLayerRenderService featureLayerRenderService = new DGIS.Service.Service.RenderService.FeatureLayerRenderService();
            IFeatureRenderer render= featureLayerRenderService.CreateUniqueRender("功能区划", dic, labels);

            IGeoFeatureLayer geoFeatureLayer = featureLayer as IGeoFeatureLayer;
            geoFeatureLayer.Renderer = render;
        }

        private void featureBreaksMenuItem_Click(object sender, EventArgs e)
        {
            IFeatureLayer featureLayer;
            if (this.axMapControl.LayerCount == 0 || (featureLayer = this.axMapControl.get_Layer(0) as IFeatureLayer) == null)
            {
                MessageBox.Show("请加载第一图层为矢量图层");
                return;
            }

            DGIS.Service.IService.IRenderService.ISymbolService symbolService = new DGIS.Service.Service.RenderService.SymbolService();
            string stylePath = @"C:\Users\Dview\Desktop\test.style";
            Dictionary<double, ISymbol> dic = new Dictionary<double, ISymbol>();
            dic.Add(10, symbolService.GetSymbolFromStyleFile(stylePath, "Fill Symbols", "核心区"));
            dic.Add(20, symbolService.GetSymbolFromStyleFile(stylePath, "Fill Symbols", "缓冲区"));
            dic.Add(30, symbolService.GetSymbolFromStyleFile(stylePath, "Fill Symbols", "实验区"));

            List<string> labels = new List<string>();
            labels.Add("<=10");
            labels.Add("<=20");
            labels.Add("<=30");

            DGIS.Service.IService.IRenderService.IFeatureLayerRenderService featureLayerRenderService = new DGIS.Service.Service.RenderService.FeatureLayerRenderService();
            IFeatureRenderer render = featureLayerRenderService.CreateClassBreaksRender("FID", dic, labels);

            IGeoFeatureLayer geoFeatureLayer = featureLayer as IGeoFeatureLayer;
            geoFeatureLayer.Renderer = render;
        }

        #endregion

        #endregion


    }
}
